package com.yx.gsyd.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yx.gsyd.entity.SysManagerRole;
import com.yx.gsyd.mapper.SysManagerRoleMapper;
import com.yx.gsyd.service.ISysManagerRoleService;
import com.yx.gsyd.util.IdentityUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-06-21
 */
@Service
public class SysManagerRoleServiceImpl extends ServiceImpl<SysManagerRoleMapper, SysManagerRole> implements ISysManagerRoleService {

    /**
     * 新增用户角色关联表
     * @param managerId
     * @param roleIds
     * @return
     */
    @Override
    public boolean saveRoleUser(String[] roleIds,String managerId) {
        //新增权限角色关联表
        List<SysManagerRole> hjManagerRoles=null;
        if(roleIds!=null&&roleIds.length>=1){
            hjManagerRoles=new ArrayList<>();
            SysManagerRole hjManagerRole=null;
            for (String roleId :roleIds) {
                hjManagerRole=new SysManagerRole();
                hjManagerRole.setManagerRoleId(IdentityUtil.identityId("MR"));
                hjManagerRole.setManagerId(managerId);
                hjManagerRole.setRoleId(roleId);
                hjManagerRoles.add(hjManagerRole);
            }
            return this.saveBatch(hjManagerRoles);
        }
        return true;
    }

    /**
     * 获取当前用户绑定的角色ID
     * @param managerId
     * @return
     */
    @Override
    public List<Object> getByRoleId(String managerId) {
        QueryWrapper<SysManagerRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("manager_id",managerId);
        queryWrapper.select("role_id");
        List<Object> hjRoleManager=this.listObjs(queryWrapper);
        return hjRoleManager;
    }

    /**
     * 删除之前关联这个账户关联的角色数据
     * @param managerId
     */
    @Override
    public boolean removeManagerRoleId(String managerId) {
        QueryWrapper<SysManagerRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("manager_id",managerId);
        return this.remove(queryWrapper);
    }

    /**
     * 获取角色集合吗
     * @param loginId
     * @return
     */
    @Override
    public List<String> getRoleList(Object loginId) {
        return this.baseMapper.getRoleList(loginId);
    }
}
